package cn.com.idmy.base;

import com.alibaba.fastjson2.annotation.JSONField;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.split.SplitUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static cn.com.idmy.base.util.TreeUtil.treeNameSeparator;


public interface TreeNode<T> extends Sortable {
    @NotNull
    Long getId();

    void setId(Long id);

    @NotNull
    Long getPid();

    void setPid(Long pid);

    @NotNull
    String getName();

    @NotNull
    String getIdsPath();

    void setIdsPath(String idsPath);

    @NotNull
    String getNamesPath();

    void setNamesPath(String namesPath);

    Boolean getIsLeaf();

    void setIsLeaf(Boolean leaf);

    Integer getLevel();

    void setLevel(Integer level);

    Long getTreeId();

    void setTreeId(Long treeId);

    @Nullable List<T> getChildren();

    void setChildren(@NotNull List<T> children);

    @NotNull
    @JSONField(serialize = false)
    default List<Long> getIdsPaths() {
        if (StrUtil.isBlank(getIdsPath())) {
            return Collections.emptyList();
        } else {
            String[] strings = SplitUtil.splitToArray(getIdsPath(), treeNameSeparator);
            Long[] longs = ConvertUtil.toLongArray(strings);
            return Arrays.stream(longs).collect(Collectors.toList());
        }
    }

    @JSONField(serialize = false)
    default List<String> getNamesPaths() {
        if (StrUtil.isBlank(getNamesPath())) {
            return Collections.emptyList();
        } else {
            return Arrays.stream(getNamesPath().split(treeNameSeparator)).collect(Collectors.toList());
        }
    }
}
